<?php
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('settings.inc');
require_once('crypt.inc');
require_once('helper.inc');

/*
0: Verwalter
1: Administrator
2: Projektleiter
3: Mitarbeiter
4: Kontroller
5: Kein Zugang!
6: Ressourcenmanager
7: Revisor
*/
$drm = array(true, false, true, true, false, true, true, true);

if (isset($_REQUEST['pjclose'])) { $knopf = 50; $headline = 41; }

require_once('menu.inc');

/*
 * Klassendefinitionen
 */
class prj_in {
   var $arr_prnum;
   var $arr_prname;
   var $arr_prstat;
   var $arr_plnum;
   var $arr_abdatum;
   var $anz;
   var $error;

   function prj_in($db=-1) {
      if ($db == -1) {
         $db = OpenDB();
         $flag = true;
      } else
         $flag = false;

      $menge = 0;
      $query = "select pr_num, pr_name, pr_status from project ";
      $query .= "order by pr_num";

      if (!($result = QueryDB($db, $query))) {
         if ($flag)
            closeDB($db);

	 $this->error = true;
	 return;
      }

      $num = numrowsDB($result);
      $i = 0;

      while ($i < $num) {
	 $data = fetchDB($result, $i);
	 $pr_num = $data[0];
	 $pr_name = $data[1];
	 $pr_status = $data[2];

	 $query = "select pl_num, pl_status from plan where ";
	 $query .= "pl_prnum = $pr_num and pl_status in (0,2,3,4) ";
	 $query .= "order by pl_lfd desc";

	 if (!($respl = QueryDB($db, $query))) {
            if ($flag)
               closeDB($db);

	    $this->error = true;
	    return;
	 }

	 if (numrowsDB($respl) > 0) {
	    $data = fetchDB($respl, 0);
	    $pl_num = $data[0];
	    $pl_status = $data[1];

	    $query = "select ab_datum from abschluss where ";
	    $query .= "ab_prnum = $pr_num order by ab_datum desc";

	    if (!($resab = QueryDB($db, $query))) {
	       if ($flag)
	          closeDB($db);

	       $this->error = true;
	       return;
	    }

	    if (numrowsDB($resab) > 0) {
	       $data = fetchDB($resab, 0);
	       $this->arr_abdatum[$menge] = $data[0];
	    } else
	       $this->arr_abdatum[$menge] = 0;

	    $this->arr_prnum[$menge] = $pr_num;
	    $this->arr_prname[$menge] = $pr_name;

	    if ($pr_status == 1)
	       $this->arr_prstat[$menge] = 0;
	    else
	       $this->arr_prstat[$menge] = $pl_status;

	    $this->arr_plnum[$menge] = $pl_num;
	    $menge++;
	 }

	 $i++;
      }

      $this->anz = $menge;

      if ($flag)
         closeDB($db);

      $this->error = false;
   }

   function iserror() {
      return $this->error;
   }

   function GetCount() {
      return $this->anz;
   }

   function GetProjectNum($prj) {
      if ($prj >= $this->anz)
	 return 0;

      return $this->arr_prnum[$prj];
   }

   function GetProjectName($prj) {
      if ($prj >= $this->anz)
	 return 0;

      return $this->arr_prname[$prj];
   }

   function GetProjectStatus($prj) {
      if ($prj >= $this->anz)
	 return 0;

      return $this->arr_prstat[$prj];
   }

   function GetProjectPlan($prj) {
      if ($prj >= $this->anz)
	 return 0;

      return $this->arr_plnum[$prj];
   }

   function GetProjectClose($prj) {
      if ($prj >= $this->anz)
         return 0;

      return $this->arr_abdatum[$prj];
   }
}

function mask_pjclose() {
	global $pj_lock;
	global $menu;
	global $headline;
	global $periode;
	global $dtformatshort;

	if (!$pj_lock) {
	   Error(GetMessage(-1, 140, "Der Teilabschlu&szlig; von Projekten ist mit der derzeitigen Einstellung nicht erlaubt!"));
	   return;
	}

	$db = OpenDB();
	$prj = new prj_in($db);

	if ($prj->iserror())
	   return;
?>
<form name="lock" action="pjclose.php" method="post">
   <input type="hidden" name="menu" value="<? echo "$menu"; ?>">
   <input type="hidden" name="headline" value="<? echo "$headline"; ?>">
   <input type="hidden" name="func" value="LockProject">
<table class="input">
   <tr>
      <th class="sel" colspan=5><? Output($db, 181, "Projekte abschlie&szlig;en"); ?></th>
   </tr>
   <tr>
      <td colspan=3><? Output($db, 186, "Projekte abschlie&szlig;en bis:"); ?></td>
      <td colspan=2>
         <select name="ab_datum">
<?
	# Juengstes Datum ermitteln
	$anz = $prj->GetCount();
	$i = 0;
	$dat = 0;

	while ($i < $anz) {
	   if ($dat < $prj->GetProjectClose($i))
	      $dat = $prj->GetProjectClose($i);

	   $i++;
	}

        # Aeltestes Datum ermittlen
	$i = 0;
	$dat_low = $dat;

	while ($i < $anz && $dat_low > 0) {
	   if ($dat_low > $prj->GetProjectClose($i))
	      $dat_low = $prj->GetProjectClose($i);

	   $i++;
	}

	# Sicherstellen, dass wir den Monatsultimo verwenden. Zusaetzlich
	# kann maximal bis zu einem Jahr in die Vergangenheit abgeschlossen
	# werden.

	if ($dat == 0) {
	   $day = gmdate("j");
	   $mon = gmdate("n");
	   $year = gmdate("Y");
	   $year--;
	   $dat = GetUltimo(gmmktime(0, 0, 0, $mon, $day, $year));
	} else {
//	   $dat = IncDate($periode, $dat);
	   $day = gmdate("j", $dat);
	   $mon = gmdate("n", $dat);
	   $year = gmdate("Y", $dat);
	   $dat = GetUltimo(gmmktime(0, 0, 0, $mon, $day, $year));
	}

	if ($dat_low == 0) {
	   if ($dat > 0)
	      $dt = $dat;
	   else
	      $dt = time();

	   $day = gmdate("j", $dt);
	   $mon = gmdate("n", $dt);
	   $year = gmdate("Y", $dt);
	   $year--;
	   $dat_low = GetUltimo(gmmktime(0, 0, 0, $mon, $day, $year));
	} else {
	   $dat_low = IncDate($periode, $dat_low);
	   $day = gmdate("j", $dat_low);
	   $mon = gmdate("n", $dat_low);
	   $year = gmdate("Y", $dat_low);
	   $dat_low = GetUltimo(gmmktime(0, 0, 0, $mon, $day, $year));
	}

	# Heutiges Datum
	$day = gmdate("j");
	$mon = gmdate("n");
	$year = gmdate("Y");
	$heute = gmmktime(0, 0, 0, $mon, $day, $year);
	$dt = $dat_low;
	$i = 0;

	while ($dt < $heute) {
	   echo "<option value=\"$dt\"";
	   $d = $dt;
	   $dt = GetUltimo(IncDate($periode, $dt));

	   if ($dt >= $heute)
	      echo " selected>";
	   else
	      echo ">";

	   echo gmdate($dtformatshort, $d);
	   echo "</option>\n";
	   $i++;
	}

	$dat_high = $d;
?>
         </select>
<?
	if ($i == 0)
	   Output($db, 188, "Derzeit sind alle Projekte bis zum %s abgeschlossen!", gmdate($dtformatshort, $dat));
?>
      </td>
   </tr>
   <tr>
      <td colspan=5>
         <table border=0>
            <tr><td>
<?
	if ($i > 0)
	   Button(GetMessage($db, 185, "Markierte Projekte Abschlie&szlig;en"), "lock");
	else
	   echo "&nbsp;";
?>
            </td><td>
<? Button(GetMessage($db, 372, "Letzten Abschl. f&uuml;r Mark. Proj. zur&uuml;cknehmen"), "lock",
	"func", "RemoveLock"); ?>
            </td></tr>
	 </table>
      </td>
   </tr>
   <tr>
      <th class="sel">&nbsp;</th>
      <th class="sel"><? Output($db, 182, "Pr.Nr."); ?></th>
      <th class="sel"><? Output($db, 326, "Art"); ?></th>
      <th class="sel"><? Output($db, 183, "Projektbezeichnung"); ?></th>
      <th class="sel"><? Output($db, 184, "Lzt.Abschl."); ?></th>
   </tr>
<?
	$i = 0;

	while ($i < $anz) {
	   echo "<tr><td class=\"selakt\"><input type=\"checkbox\" ";
	   echo "name=\"check_" . $prj->GetProjectNum($i) . "\"";

	   if ($prj->GetProjectClose($i) < $dat_high)
	      echo " checked";
	      
	   echo "></td>\n";
	   echo "<td class=\"sel\">" . $prj->GetProjectNum($i) . "</td>\n";
	   echo "<td class=\"sel\">";

	   switch ($prj->GetProjectStatus($i)) {
	      case 0: Output($db, 234, "Konto"); break;
	      default: Output($db, 262, "Projekt");
	   }

	   echo "</td>\n";
	   echo "<td class=\"sel\">" . $prj->GetProjectName($i) . "</td>\n";

	   if ($prj->GetProjectClose($i) == 0)
	      $datum = "";
	   else
	      $datum = gmdate($dtformatshort, $prj->GetProjectClose($i));

	   echo "<td class=\"sel\">$datum</td></tr>\n";
	   $i++;
	}
	
	echo "</table></form>\n";
	closeDB($db);
}

function LockProjects() {
	global $unum;
	$ab_datum = $_REQUEST['ab_datum'];

	if (!isset($ab_datum) || $ab_datum < 1) {
	   Error(GetMessage(-1, 187, "Interner Fehler: LockProjects: Abschlu&szlig;datum fehlt!"));
	   return;
	}

	$db = OpenDB();
	$prj = new prj_in($db);

	if ($prj->iserror()) {
	   closeDB();
	   return;
	}

	$query = "select co_abschluss from counter";

	if (!($result = QueryDB($db, $query))) {
	   closeDB($db);
	   return;
	}

	$data = fetchDB($result, 0);
	$co_abschluss = $data[0] + 1;
	$anz = $prj->GetCount();
	$i = 0;
	$query = "insert into abschluss (ab_num, ab_datum, ab_user, ab_prnum, ab_plnum) ";
	$query .= "values (";

	if (!QueryDB($db, "begin")) {
	   closeDB($db);
	   return;
	}

	while ($i < $anz) {
	   $cname = "check_" . $prj->GetProjectNum($i);
	   $check = $_REQUEST[$cname];

	   if (!isset($check)) {
	      $i++;
	      continue;
	   }

	   # Abschluss in die Tabelle "abschluss" schreiben
	   $q = "$co_abschluss, $ab_datum, $unum, ";
	   $q .= $prj->GetProjectNum($i) . ", ";
	   $q .= $prj->GetProjectPlan($i) . ")";

	   if (!QueryDB($db, $query . $q)) {
	      QueryDB($db, "rollback");
	      closeDB($db);
	      return;
	   }

	   # Alle IST-Buchungen bis zum Abschlussdatum auf NICHT editierbar
	   # setzen.
	   $q = "update wdone set wd_edit = 'false' where ";
	   $q .= "wd_prnum = " . $prj->GetProjectNum($i) . " and ";
	   $q .= "wd_datum <= $ab_datum";

	   if (!QueryDB($db, $q)) {
	      QueryDB($db, "rollback");
	      closeDB($db);
	      return;
	   }

	   $i++;
	   $co_abschluss++;
	}

	if (!QueryDB($db, "update counter set co_abschluss = $co_abschluss")) {
	   QueryDB($db, "rollback");
	   closeDB($db);
	   return;
	}

	if (!QueryDB($db, "commit"))
	   QueryDB($db, "rollback");

	closeDB($db);
}

function RemoveLock() {
	global $unum;

	$db = OpenDB();
	$prj = new prj_in($db);

	if ($prj->iserror()) {
	   closeDB();
	   return;
	}

	$anz = $prj->GetCount();
	$i = 0;

	if (!QueryDB($db, "begin")) {
	   closeDB($db);
	   return;
	}

	while ($i < $anz) {
	   $cname = "check_" . $prj->GetProjectNum($i);
	   $check = $_REQUEST[$cname];

	   if (!isset($check)) {
	      $i++;
	      continue;
	   }

	   $query = "select ab_num, ab_datum from abschluss where ab_prnum = ";
	   $query .= $prj->GetProjectNum($i) . "order by ab_datum desc";

	   if (!($result = QueryDB($db, $query))) {
	      closeDB($db);
	      return;
	   }

	   $menge = numrowsDB($result);

	   if ($menge > 0) {
	      $data = fetchDB($result, 0);
	      $ab_num = $data[0];

	      if ($menge > 1) {
		 $data = fetchDB($result, 1);
		 $last_abdatum = $data[1];
	      } else
		 $last_abdatum = 0;

	      $query = "delete from abschluss where ab_num = $ab_num";

	      if (!QueryDB($db, $query)) {
		 QueryDB($db, "rollback");
		 closeDB($db);
		 return;
	      }

	      $query = "update wdone set wd_edit = true where ";
	      $query .= "wd_prnum = " . $prj->GetProjectNum($i) . " and ";
	      $query .= "wd_datum > $last_abdatum";

	      if (!QueryDB($db, $query)) {
		 QueryDB($db, "rollback");
		 closeDB($db);
		 return;
	      }
	   }

	   $i++;
	}

	QueryDB($db, "commit");
	closeDB($db);
}

if ($knopf == 50)
   mask_pjclose();

$func = $_REQUEST['func'];

if ($func == "RemoveLock") {
   $func = "";
   RemoveLock();
   mask_pjclose();
}

if ($func == "LockProject") {
   LockProjects();
   mask_pjclose();
}

require_once('footer.inc');
